package com.kyyc.luff.auth.config;

import com.kyyc.luff.auth.model.LoginUser;
import com.kyyc.luff.core.Constant;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义token生成携带的信息
 */
public class RedisTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        final Map<String, Object> additionalInfo = new HashMap<>();

        LoginUser loginUser = (LoginUser) authentication.getUserAuthentication().getPrincipal();
        additionalInfo.put(Constant.DETAILS_USER_ID, loginUser.getId());
        additionalInfo.put(Constant.DETAILS_USERNAME, loginUser.getUsername());
        additionalInfo.put(Constant.DETAILS_DEPT_ID, loginUser.getDeptId());

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }

}
